package com.happycat.utils;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.UUID;
import com.happycat.SysContants;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

@Slf4j
public class AesUtils {
	 /**
     * 加密
     *
     * @param data 需要加密的内容
     * @param key  加密密码
     * @return
     */
    public static byte[] encrypt(byte[] data, byte[] key) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key, SysContants.AES);
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, SysContants.AES);
            Cipher cipher = Cipher.getInstance(SysContants.AES_CBC_CIPHER);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
            return cipher.doFinal(data);
        } catch (Exception e) {
        	System.out.println("AES加密错误！");
        }
        return null;
    }

    /**
     * 解密
     *
     * @param data 待解密内容
     * @param key  解密密钥
     * @return
     */
    public static byte[] decrypt(byte[] data, byte[] key) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key, SysContants.AES);
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, SysContants.AES);
            Cipher cipher = Cipher.getInstance(SysContants.AES_CBC_CIPHER);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
            return cipher.doFinal(data);
        } catch (Exception e) {
        	System.out.println("AES解密错误！");
        }
        return null;
    }

    /**
     * 加密
     *
     * @param data 需要加密的内容
     * @param key  加密密码
     * @return
     */
    public static String encrypt(String data, String key) {
        try {
            byte[] valueByte = encrypt(data.getBytes(SysContants.UTF_8), key.getBytes(SysContants.UTF_8));
            return Base64.getEncoder().encodeToString(valueByte);
        } catch (UnsupportedEncodingException e) {
            System.out.println("AES加密错误！");
        }
        return null;
    }

    /**
     * 解密
     *
     * @param data 待解密内容 base64 字符串
     * @param key  解密密钥
     * @return
     */
    public static String decrypt(String data, String key) {
        log.info("data:{}", data);
        try {
            byte[] originalData = Base64.getDecoder().decode(data.getBytes());
            byte[] valueByte = decrypt(originalData, key.getBytes(SysContants.UTF_8));
            return new String(valueByte);
        } catch (UnsupportedEncodingException e) {
        	 System.out.println("AES加密错误！");
        }
        return null;
    }

    /**
     * 生成一个随机字符串密钥
     *
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String generateRandomKey() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16).toUpperCase();
    }
	
	public static void main(String[] args) {
		String randomKey = AesUtils.generateRandomKey();
		// 随机密钥加密
		/*
		 * String username = AesUtils.encrypt("admin", randomKey); String url =
		 * AesUtils.encrypt(
		 * "jdbc:mysql://47.104.97.252:3306/happy_cat?characterEncoding=utf8&useSSL=false",
		 * randomKey); String passwrod = AesUtils.encrypt("123456789kl",
		 * "F4926CA4E64E485F"); String phone = AesUtils.encrypt("13539447522",
		 * "F4926CA4E64E485F");
		 * 
		 * System.out.println("key: "+randomKey);
		 * System.out.println("username: mpw:"+username);
		 * System.out.println("url: mpw:"+url);
		 * System.out.println("passwrod: mpw:"+passwrod);
		 * System.out.println("phone: mpw:"+phone);
		 */
//		String depassword = AesUtils.decrypt("1yrSxdrDwE3rSkc1AC8qgQ==", "4953cdb84c3fbcb7");
//		String depassword = AesUtils.encrypt("123456789kl", "F4926CA4E64E485F");
//		String ddd = AesUtils.encrypt(depassword, "642E6FD80A054341");
//		String depassword = AesUtils.decrypt("5Ync9uam8DFmOPqPT3FcYg==", "642E6FD80A054341");
        String frontAes = "F4926CA4E64E485F";
        //手机号加密
//		String phone = AesUtils.encrypt("13539447522", frontAes);
		String phone = AesUtils.encrypt("13539447522", frontAes);

		//密码
		String enP = AesUtils.decrypt("5Ync9uam8DFmOPqPT3FcYg==", "642E6FD80A054341");
		String frontEnP = AesUtils.encrypt(enP, frontAes);

		System.out.println("phone:" + phone);
		System.out.println("frontEnP:" + frontEnP);

        System.out.println(System.currentTimeMillis());

        System.out.println("sdfd:"+decrypt("qDpCmwcxqGuLxjZC/Wkn8A==", frontAes));
        System.out.println("sdfd:"+decrypt("BLy++TA+pRshXOoc6DSlqw==", frontAes));
	}
}
